with Ada.Exceptions;

with GWindows.Application;
with GWindows.Base;
with GWindows.Windows;         use GWindows.Windows;
with GWindows.Windows.Main;    use GWindows.Windows.Main;
with GWindows.Buttons;         use GWindows.Buttons;
with GWindows.Edit_Boxes;      use GWindows.Edit_Boxes;

with GUI;                      use GUI;
with GUI.Log;                  use GUI.Log;

with Managed;

separate(ffxialarm)
package body GUI_Events is
  procedure Do_Events is
    Can_Close_Log : Boolean := False;
    ------------------------------
    -- Event handler procedures --
    ------------------------------

    procedure Do_Start (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Tasks.Start_Stop_Logger;
    end Do_Start;

    procedure Do_Display_Log (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Display_Log(Log_Window);
    end Do_Display_Log;

    procedure Do_Close_Log (Window    : in out GWindows.Base.Base_Window_Type'Class;
                            Can_Close :    out Boolean) is
    begin
      Can_Close := Can_Close_Log;
      if not Can_Close_Log then
        Display_Log(Log_Window);
      end if;
    end Do_Close_Log;

    procedure Do_Resize_Log (Window : in out GWindows.Base.Base_Window_Type'Class;
                             Width  : in     Integer;
                             Height : in     Integer) is
    begin
      Resize_Log(Log_Window);
    end Do_Resize_Log;

    procedure Do_Retrieve_Config
      (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Retrieve_Config;

    procedure Do_Edit1 (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      on_edit(Main_Window, 1);
      Tasks.Stop_Logger;
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Edit1;

    procedure Do_Edit2 (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      on_edit(Main_Window, 2);
      Tasks.Stop_Logger;
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Edit2;

    procedure Do_Edit3 (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      on_edit(Main_Window, 3);
      Tasks.Stop_Logger;
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Edit3;

    procedure Do_Edit4 (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      on_edit(Main_Window, 4);
      Tasks.Stop_Logger;
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Edit4;

    procedure Do_Edit5 (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      on_edit(Main_Window, 5);
      Tasks.Stop_Logger;
      Managed.Config.set(Retrieve_Config(Main_Window));
    end Do_Edit5;

    procedure Do_Minimize (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Minimize(Main_Window);
    end Do_Minimize;

    procedure Do_Maximize (Window : in out GWindows.Base.Base_Window_Type'Class;
                           X      : in     integer;
                           Y      : in     integer;
                           Keys   : in     Mouse_Key_States) is
    begin
      Maximize(Main_Window);
    end Do_Maximize;

    procedure Do_Display_Menu (Window : in out GWindows.Base.Base_Window_Type'Class;
                               X      : in     integer;
                               Y      : in     integer;
                               Keys   : in     Mouse_Key_States) is
    begin
      Display_Tray_Menu(Main_Window, X, Y);
    end Do_Display_Menu;

    procedure Do_Menu_Select (Window : in out GWindows.Base.Base_Window_Type'Class;
                              Item   : in     Integer) is
    begin
      Process_Menu_Command(Main_Window, Item);
      if Item = GUI.Menu_Commands.MC_START then
        Maximize(Main_Window);
        Tasks.Start_Stop_Logger;
      elsif Item = GUI.Menu_Commands.MC_CLOSE then
        Close_Window(Main_Window);
      else
        Managed.Config.set(Retrieve_Config(Main_Window));
      end if;
    end Do_Menu_Select;

    procedure Do_Close (Window    : in out GWindows.Base.Base_Window_Type'Class;
                        Can_Close :    out Boolean) is
    begin
      Tasks.Stop_Logger;
      Can_Close_Log := True;
      Close(Log_Window);
      Can_Close := True;
    end Do_Close;

    procedure Do_Filter (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Filter(Log_Window);
    end Do_Filter;

    procedure Do_Export (Window : in out GWindows.Base.Base_Window_Type'Class) is
    begin
      Export_Filtered(Log_Window);
    end Do_Export;
  begin
    create_window (Main_Window);
    Create_Log_Window (Log_Window);

    --------------------
    -- Event handlers --
    --------------------

    On_Click_Handler                 (Main_Window.StartStop,
                                      Do_Start'Unrestricted_Access);
    -- Start/Stop button has been clicked
    On_Click_Handler                 (Main_Window.ViewLog,
                                      Do_Display_Log'Unrestricted_Access);
    -- Event Log button has been pressed

    On_Click_Handler                 (Main_Window.Loop_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    On_Click_Handler                 (Main_Window.Tell_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    On_Click_Handler                 (Main_Window.LS_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    On_Click_Handler                 (Main_Window.Party_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    On_Click_Handler                 (Main_Window.Say_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    On_Click_Handler                 (Main_Window.Shout_Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    for i in Main_Window.Key_Alarms'Range loop
      On_Click_Handler               (Main_Window.Key_Alarms(i).Chk,
                                      Do_Retrieve_Config'Unrestricted_Access);
    end loop;
    -- Checkboxes had been changed
    On_Change_Handler                (Main_Window.Key_Alarms(1).Edit,
                                      Do_Edit1'Unrestricted_Access);
    On_Change_Handler                (Main_Window.Key_Alarms(2).Edit,
                                      Do_Edit2'Unrestricted_Access);
    On_Change_Handler                (Main_Window.Key_Alarms(3).Edit,
                                      Do_Edit3'Unrestricted_Access);
    On_Change_Handler                (Main_Window.Key_Alarms(4).Edit,
                                      Do_Edit4'Unrestricted_Access);
    On_Change_Handler                (Main_Window.Key_Alarms(5).Edit,
                                      Do_Edit5'Unrestricted_Access);
    -- Textboxes had been edited
    On_Minimize_Handler              (Main_Window.Top_Window,
                                      Do_Minimize'Unrestricted_Access);
    -- The window has been minimized
    On_Tray_Icon_Click_Handler       (Main_Window.Top_Window,
                                      Do_Maximize'Unrestricted_Access);
    -- The System Tray Icon has been Left clicked
    On_Tray_Icon_Right_Click_Handler (Main_Window.Top_Window,
                                      Do_Display_Menu'Unrestricted_Access);
    -- The System Tray Icon has been Right clicked
    On_Menu_Select_Handler           (Main_Window.Top_Window,
                                      Do_Menu_Select'Unrestricted_Access);
    -- The System tray context menu has been clicked
    On_Close_Handler                 (Main_Window.Top_Window,
                                      Do_Close'Unrestricted_Access);

    On_Click_Handler                 (Log_Window.Export,
                                      Do_Export'Unrestricted_Access);
    On_Close_Handler                 (Log_Window.Window,
                                      Do_Close_Log'Unrestricted_Access);
    On_Size_Handler                  (Log_Window.Window,
                                      Do_Resize_Log'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Alarm_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Tell_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.LS_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Party_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Say_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Shout_Chk,
                                      Do_Filter'Unrestricted_Access);
    On_Click_Handler                 (Log_Window.Other_Chk,
                                      Do_Filter'Unrestricted_Access);

    GWindows.Application.Message_Loop;
  end Do_Events;
end GUI_Events;
